﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using SilverPOP.Data.Process.Entity;
using SilverPOP.Data.Process.BLL;
using Helpers;
using Utilities;
using Atp.Net;
using SilverPOP.Library.BusinessEntities;
using System.Xml;
using System.Threading;
using SilverPOP.Library.Helpers;
using Ionic.Zip;
using LumenWorks.Framework.IO.Csv;
using SilverPOP.Data.Process.DAL;


namespace SilverPOP.Data.Process.BLL
{
    public class SilverPOPAggregateReportBLL : BaseBLL
    {
        

        #region Get Active SilverPOP Report IDs By Blast ID
        public static List<SilverPOPReportIDInfo> GetSilverPOPActiveReportIDs(long blastId)
        {

            List<ParamWithValue> spParamValue = new List<ParamWithValue>();
            spParamValue.Add(new ParamWithValue("@BlastId", blastId));

            List<DataReaderObject> dataList = DAL.DAL.ExecuteStoredProcedureWithParametersAndGetDataObject("SPR_Get_Active_SilverPOP_Report_Ids", spParamValue);

            if (dataList != null)
            {

                List<SilverPOPReportIDInfo> silverPOPReportIdList = new List<SilverPOPReportIDInfo>();

                foreach (DataReaderObject dataReader in dataList)
                {
                    SilverPOPReportIDInfo reportIdInfo = new SilverPOPReportIDInfo();

                    reportIdInfo.BlastId = (long)dataReader.ColumnValueList[0];
                    reportIdInfo.SegmentId = (long)dataReader.ColumnValueList[1];
                    reportIdInfo.MailingId = (long)dataReader.ColumnValueList[2];
                    reportIdInfo.ReportId = dataReader.ColumnValueList[3] != DBNull.Value ? (long)dataReader.ColumnValueList[3] : 0;

                    silverPOPReportIdList.Add(reportIdInfo);
                    
                }

                return (silverPOPReportIdList != null && silverPOPReportIdList.Count > 0) ? silverPOPReportIdList : null;
            }

            return null;
        }
        #endregion
       
      public static void UpdateSilverPOPReportIDInDatabase(long blastId,long schedulerMailingId,long silverPOPReportId,DateTime modifiedDate)
      {
          List<ParamWithValue> spParamValue = new List<ParamWithValue>();
          spParamValue.Add(new ParamWithValue("@BlastId", blastId));
          spParamValue.Add(new ParamWithValue("@SchedulerMailingId", schedulerMailingId));
          spParamValue.Add(new ParamWithValue("@SilverPOPReportId", silverPOPReportId));
          spParamValue.Add(new ParamWithValue("@ModifiedOn", modifiedDate));

          List<DataReaderObject> dataList = DAL.DAL.ExecuteStoredProcedureWithParametersAndGetDataObject("SPR_Update_Report_ID", spParamValue);

      }

      public static void InsertSilverPOPSummaryDataInDatabase(SummaryReport summaryReport,DateTime createdDate)
      {
          List<ParamWithValue> spParamValue = new List<ParamWithValue>();
          spParamValue.Add(new ParamWithValue("@BlastId", summaryReport.BlastID));
          spParamValue.Add(new ParamWithValue("@TotalSent", summaryReport.SentCount));
          spParamValue.Add(new ParamWithValue("@TotalBounced", summaryReport.BouncedCount));
          spParamValue.Add(new ParamWithValue("@TotalOpened", summaryReport.OpenedCount));
          spParamValue.Add(new ParamWithValue("@TotalClicked", summaryReport.ClickedCount));
          spParamValue.Add(new ParamWithValue("@TotalOptout", summaryReport.OptOutCount));
          spParamValue.Add(new ParamWithValue("@DateCreated", createdDate));

          List<DataReaderObject> dataList = DAL.DAL.ExecuteStoredProcedureWithParametersAndGetDataObject("SPR_Insert_SilverPOP_Aggregate_Summary", spParamValue);

      }

      public static void InsertUniqueEventTypeCount(SummaryReport summaryReport)
      {
          List<ParamWithValue> spParamValue = new List<ParamWithValue>();
          spParamValue.Add(new ParamWithValue("@BlastId", summaryReport.BlastID));
          spParamValue.Add(new ParamWithValue("@SegmentId", summaryReport.SegmentID));
          spParamValue.Add(new ParamWithValue("@SchedulerMailingId", summaryReport.MailingID));
          spParamValue.Add(new ParamWithValue("@UniqueOpen", summaryReport.UniqueOpen));
          spParamValue.Add(new ParamWithValue("@UniqueClick", summaryReport.UniqueClick));

          List<DataReaderObject> dataList = DAL.DAL.ExecuteStoredProcedureWithParametersAndGetDataObject("SPR_Set_Unique_Event_Type_Count", spParamValue);

      }


      public static void ProcessSilverPopAggregateSummary(long campaignId)
      {
          List<ParamWithValue> spParamValue = new List<ParamWithValue>();
          spParamValue.Add(new ParamWithValue("@CampaignId", campaignId));
          List<DataReaderObject> dataList = DAL.DAL.ExecuteStoredProcedureWithParametersAndGetDataObject("SPR_Process_SilverPop_Aggregate_Summary", spParamValue);

      }
    
    }//end class
}
